'use strict';
/**
 * 角色权限管理Service
 */
app.service('RoleService', 
['$http', '$q',
function ($http) {
	this.gridOptions = function(){
		var _gridWidth = 1024;
		var _columnWidth = _gridWidth / 3;
		return {
			//height: getGridHeight(),
			url: ISS.getPath("/role/rolelist"),
			dataType: 'json',
			colModel : [
				{display: 'id', name : 'id', width : 50,hide: true},
				{display: 'role.name', name : 'name', width : _columnWidth},
				{display: 'common.remark', name : 'description', width : _columnWidth},
				{display: 'common.createTime', name : 'createTime', width : 200},
				{display: 'common.operation', name : 'id', width : _columnWidth / 2,process:function(v,index){
					var html = [];
					html.push('<button iss-permission="B_ROLE_UPDATE" class="btn m-r-xs btn-xs btn-primary" id="updateRoleBtn" ng-click="vm.updateRole('+v+')">');
					html.push('  <i class="fa fa-pencil"></i>');
					html.push('  <span translate="common.edit"></span>');
					html.push('</button>');
					html.push('<button iss-permission="B_ROLE_DELETE" class="btn m-r-xs btn-xs btn-danger" id="delRoleBtn" ng-click="vm.delRole('+index+')">');
					html.push('  <i class="fa fa-trash-o"></i>');
					html.push('  <span translate="common.del"></span>');
					html.push('</button>');
					return html.join('');
				}}
			],
			usepager: true,
			showcheckbox: true,
			rp: 10
		};
	}
	this.addRole = function(role){
		return $http.post(
				ISS.getPath('/role/add'),
				role,
				{contentType: 'json'}
			).then(function(resp){
				return resp.data;
			});
	}
	this.initUpdateRole = function(roleId){
		return $http.post(
				ISS.getPath('/role/get/'+roleId)
			).then(function(resp){
				return resp.data;
			});
	}
	this.updateRole = function(role){
		return $http.post(
				ISS.getPath('/role/update'),
				role,
				{contentType: 'json'}
			).then(function(resp){
				return resp.data;
			});
	}
	this.deleteRole = function(data){
		return $http.post(
					ISS.getPath('/role/del/'+ data.id + '/' + data.name)
				).then(function(resp){
					return resp.data;
				});	
	}
}]);